class Solution 
{
private:
    // 计算每个位置上的数字的平方和
    inline static int BitSum(int num)
    {
        int ret = 0;
        while(num)
        {
            int t = num % 10;
            ret += t * t;
            num /= 10;
        }
        return ret;
    }

public:
    bool isHappy(int n) 
    {
        int slow = n, fast = BitSum(n);
        while(slow != fast)
        {
            slow = BitSum(slow);
            fast = BitSum(BitSum(fast));
        }
        return slow == 1;
    }
};
